//
//  HMStatusRetweetView.swift
//  sinaweibo
//
//  Created by WGP on 16/4/15.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMStatusRetweetView: UIView {

    var retweetStatus: HMStatus?
    {
        didSet
        {
            let retweetName = retweetStatus?.user?.screen_name ?? "被转发微博的用户名称"
            let retweetContent = NSMutableAttributedString(string: "@\(retweetName)")
            let retweetText = retweetStatus?.attrText ?? NSAttributedString(string: "被转发微博的内容为空")
            
            retweetContent.appendAttributedString(retweetText)
            retweetLabel.attributedText = retweetContent
            
//            设置配图
            retweetPictureView.pic_urlsArr = retweetStatus?.pic_urlsArr
            
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus?.pic_urlsArr?.count == nil ? 0 : HMStatusCellMargin
                
                make.top.equalTo(retweetLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    
//    MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        准备子控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - 私有方法
    private func setupUI()
    {
//        1.添加子空间
        addSubview(bgButton)
        addSubview(retweetLabel)
        addSubview(retweetPictureView)
        
//        2.添加约束
        
        bgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(retweetLabel.snp_bottom).offset(HMStatusCellMargin)
            
            make.size.equalTo(CGSizeZero)
        }
        
//        添加底部约束
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView.snp_bottom).offset(HMStatusCellMargin)
        }
    }
    
//    MARK: - 懒加载控件
//    背景按钮
    private lazy var bgButton: UIButton =
    {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: .Highlighted)
        
        return button
    }()
    
//    被转发的文字
    private lazy var retweetLabel: FFLabel =
    {
        let label = FFLabel(fontSize: HMTextLabelFontSize, textColor: UIColor.darkGrayColor())
        
        label.numberOfLines = 0
        
        return label
    }()
    
//    被转发的配图
    private lazy var retweetPictureView: HMStatusPictureView = HMStatusPictureView()
}







